#include "mod_config.h"
#include <glib-2.0/glib.h>

PeerConfig *loadPeerConfig(char *nomefile){


  PeerConfig *conf;
  GKeyFile *keyfile;
  GKeyFileFlags flags;
  GError *error = NULL;
  gsize length;

  /* Create a new GKeyFile object and a bitwise list of flags. */
  keyfile = g_key_file_new ();
  flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

  /* Load the GKeyFile from keyfile.conf or return. */
  if (!g_key_file_load_from_file (keyfile, nomefile, flags, &error))
  {
    g_error (error->message);
  }

  /* Create a new Settings object. If you are using GTK+ 2.8 or below, you should
   * use g_new() or g_malloc() instead! */
  conf = g_slice_new (PeerConfig);

  /* Read in data from the key file from the group "username". */
  conf->port = g_key_file_get_integer             (keyfile, "configurazionePeer",
                                                  "port", NULL);
  conf->sharedir = g_key_file_get_locale_string     (keyfile, "configurazionePeer",
                                                  "sharedir", &length, NULL);
  conf->maxdiskspace = g_key_file_get_integer    (keyfile, "configurazionePeer",
                                                  "maxdiskspace", NULL);
  conf->spportlisten = g_key_file_get_integer    (keyfile, "configurazionePeer",
                                                  "spportlisten", NULL);
  conf->spipaddress = g_key_file_get_locale_string     (keyfile, "configurazionePeer",
                                                  "spipaddress", &length, NULL);

  return conf;

}

PeerConfig *loadSuperPeerConfig(char *nomefile){


  SpConfig *conf;
  GKeyFile *keyfile;
  GKeyFileFlags flags;
  GError *error = NULL;
  gsize length;

  /* Create a new GKeyFile object and a bitwise list of flags. */
  keyfile = g_key_file_new ();
  flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

  /* Load the GKeyFile from keyfile.conf or return. */
  if (!g_key_file_load_from_file (keyfile, nomefile, flags, &error))
  {
    g_error (error->message);
  }

  /* Create a new Settings object. If you are using GTK+ 2.8 or below, you should
   * use g_new() or g_malloc() instead! */
  conf = g_slice_new (SpConfig);

  /* Read in data from the key file from the group "username". */
  conf->portlistenpeer = g_key_file_get_integer    (keyfile, "configurazioneSuperPeer",
                                                  "PortListenPeer", NULL);
  conf->portlistensp = g_key_file_get_integer      (keyfile, "configurazioneSuperPeer",
                                                  "PortListenSp", NULL);
  conf->sharedir = g_key_file_get_locale_string     (keyfile, "configurazioneSuperPeer",
                                                  "sharedir", &length, NULL);

  return conf;

}
